<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;
use Session;
use App\Member;
class TestController extends Controller
{

    public function test(){
        return "<h1>你好控制器</h1>";
    }
    public function input(Request $request){
        $name = $request -> input('name');
        $age = $request -> input('age');
        return 'name的值为'.$name.",".'age的值为'.$age;
    }
    public function input2(Request $request){
        $name = $request -> name;
        echo "name的值为".$name;
    }

    public function database(){
        $data2 = [
            "name"=>"tom",
            "age"=>"22",
            "email"=>"tom@laravel.test"
        ];
        //添加一条
//        DB::table("member")->insert($data2 );
//        $data1 = DB::table("member")->get();
//        foreach ($data1 as $v){
//            dump($v->id."-".$v->name);
//        }


        //添加多条
        $data3 = [
            [
                "name"=>"tom",
                "age"=>"22",
                "email"=>"tom@laravel.test"
            ],
            [
                "name"=>"tom",
                "age"=>"22",
                "email"=>"tom@laravel.test"
            ]
        ];
        dump(DB::table("member")->insert($data3));
//
//        //获取自增id值
//        $data4 = [
//            "name"=>"tom",
//            "age"=>"22",
//            "email"=>"tom@laravel.test"
//        ];
//        dump(DB::table("member")->insertGetId($data4));
//
//        //修改数据-> 将表中所有记录的name字段的值都改为tom
//        $data5 = ["name" => "tom"];
//        dump(DB::table("member")->update($data5));
//
//        //对数字进行递增
//        //将表中所有记录的age字段的值都加1
//        dump(DB::table("member")->increment("age"));
//        //将表中所有记录的member字段的值都加5
//        dump(DB::table("member")->increment("age",5));
//
//        //递减
//        dump(DB::table("member")->decrement("age"));
//        dump(DB::table("member")->decrement("age",2));
//
//        //where修改数据--字段名,运算符,字段值
        $data6 = [
            "name"=>"叶",
            "age"=>"12",
            "email"=>"2246106715@qq.com"
        ];
//        DB::table("member")->where("id","=","1")->update($data6);
//        DB::table("member")->where("id","1")->update($data6);
//        DB::table("member")->where(["id"=>2])->update($data6);
//        DB::table("member")->where(["id"=>1])->where(["name"=>"小叶"])->update($data6);
        DB::table("member")->where(["id"=>3])->orWhere(["name"=>"tom"])->update($data6);
    }

    //查询数据
    public function select(){
        $data = DB::table("member")->get();
        foreach ($data as $v){
            echo $v->id."-".$v->name."<br>";
        }
        $data = DB::table('member')->where("id","1")->first();
        dump($data->id);
        echo $data->id;
        echo "<br>";

        $data = DB::table('member')->get(['name','email']);
        foreach ($data as $v){
            echo    $v->name.'-'.$v->email."<br>";
        }

        echo "<br>";
        $data = DB::table("member")->first(['name','email']);
        dump($data);

        $name = DB::table("member")->where("id","3")->value("name");
        dump($name);

        echo "降序<br>";
        $data = DB::table("member")->orderBy("age","desc")->get();
        dump($data);

        echo "分页<br>";
        //从索引为5开始(不包括5)获取三条数据
        $data = DB::table("member")->limit(3)->offset(5)->get();
        dump($data);


    }

    //删除数据
    public function del(){
        $res = DB::table('member')->where('id','<','3')->delete();
        dump($res);
        //清空数据表
        DB::table('member')->truncate();

    }

    //sql语句
    public function sql(){
        $data = DB::select('select * from `member`');
        dump($data);
        echo "<br>";
        dump(DB::insert('insert into `member` set `name`= \'YE\', `age`= \'20\' ,`email`=\'2216\' '));
        echo "<br>";
        DB::update('update `member` set `name`=\'timi\' where `id`>=2 and `id`<=3 ');

        dump(DB::delete('delete from `member` where `id` >= 5'));

        DB::statement('create table if not exists `test` (`id` int)');
    }

    //渲染视图
    public function show(){
        $day = "三";
        $date = date('Y-m-d H:i:s',time());
        $time = strtotime("+1 year");
//        $conten = "文本内容";
//        $arr = [1,2];
//        $data = compact("conten",'arr');
//        dump($data);
        $arr = ['星期1','2','星期3','4','5','6','7'];
        return view("show",compact('date','day','time','arr'));
    }

    //防御CSRF攻击
    public function demo2(){
        return view('form');
    }
    public function demo3(Request $request){
        $name = $request->input('name');
        $money = $request->input('money');

        return "转账成功!成功付款".$money."元";
    }

    //模型
    public function demo(){
        $member = new Member();
        $member->name = 'save';
        $member->age = '20';
        $member->email = '2246106715';
        dump($member->save());
        dump($member->id);
        echo "<br>";
        $data = ['name'=>'fill','age'=>'20',"email"=>"119110"];
        $member->fill($data);
        $member->save();

        $data = ['name'=>'小明','age'=>'20','email'=>'9999'];
        $member = Member::create($data);
        $member->save();

        //find()方法根据主键查询记录
        $member = Member::find(3);
        dump($member->name);
        dump($member->toArray());

        $member = Member::where('id','1')->select('name','age')->find(1);
        dump($member->toArray());
    }
    public function test2(){
//        $member = new Member();
        $members = Member::where('id','1')->get();
        dump($members);
        dump(get_class($members[0]));
        $members = Member::all();
        dump($members->toArray());
        $members = Member::all(['name','age']);
        dump($members->toArray());

        echo '<br>';

        //先查询后修改
        $members = Member::find(15);
        if ($members){
            $members->name = 'test';
            $members->email = 'test@laravel.test';
            $members->save();
            dump('修改成功');
        }else{
            dump('修改失败,记录不存在');
        }

        //直接修改
        dump(Member::where('id',4)->update(['name'=>'Ye','age'=>'20']));

        $members = Member::find(15);
        if ($members){
            $members->delete();
        }else{
            dump("删除失败：记录不存在");
        }

        dump(Member::where('id',31)->delete());
    }

    //表单校验
    public function edit(){
        return view('edit');
    }
    public function save(Request $request){
        $this->validate($request,[
           'name'=>'required|min:2|max:20|unique:member',
            'age'=>'required|integer|min:1|max:120',
            'email'=>'required|email'
        ]);
        return '表单提交成功';
    }

    //Session
    public function data(){
        $data = Member::all();//查询所有记录
        Session::put('name','明明白白');// 写入session
//        dump(Session::get('name'));
//        dump(Session::all());
//        dump(Session::forget('name'));//删除名称为name的Session
        dump(Session::has('name'));//判断是否存在
        return response()->json($data);
    }

    //数据分页
    public function data2(){
        $data = Member::paginate(2);
        return view('demo',compact("data"));
    }

}

